from BladeFerm.Lib.Common import logger
from qturboframe.public_ui.base_element import BaseElement


class HomeTab(BaseElement):
    '''首页的tab标签类'''

    def __init__(self, tab_name, father, timeout=5):
        super().__init__({'css selector': 'div.tabs-view-container'}, father, timeout=timeout)
        self.topdiv = self._element
        modules = self.topdiv.find_elements_by_attribute(
            'css selector', 'div.tags-inner-scroll-body>a.tabs-view', timeout=self.timeout)
        for module in modules:
            text = module.text.strip()
            if text == tab_name:
                self._element = module
                break
        else:
            self._element = None

    def select(self):
        '''选择选项卡'''
        self._element.secure_click()
        logger.info('打开选项卡。')

    def close(self):
        '''关闭当前选项卡'''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'ul.h-tag-close-tip', timeout=self.timeout)
        upul.find_element_by_attribute('css selector', 'li.h-tag-close-tip-item>span', 'text:关闭当前选项卡', timeout=self.timeout).secure_click()
        logger.info('关闭当前选项卡。')

    def close_others(self):
        '''关闭当前意外选项卡'''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'ul.h-tag-close-tip', timeout=self.timeout)
        upul.find_element_by_attribute('css selector', 'li.h-tag-close-tip-item>span', 'text:关闭当前以外选项卡', timeout=self.timeout).secure_click()
        logger.info('关闭当前以外选项卡。')

    def close_all(self):
        '''关闭全部选项卡'''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'ul.h-tag-close-tip', timeout=self.timeout)
        upul.find_element_by_attribute('css selector', 'li.h-tag-close-tip-item>span', 'text:关闭全部选项卡', timeout=self.timeout).secure_click()
        logger.info('关闭全部选项卡。')

    def reload(self):
        '''刷新当前页面'''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'ul.h-tag-close-tip', timeout=self.timeout)
        upul.find_element_by_attribute('css selector', 'li.h-tag-close-tip-item>span', 'text:刷新当前页面', timeout=self.timeout).secure_click()